﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
using SkyApm.Tracing;
using Newtonsoft.Json;
using SkyApm.Tracing.Segments;

namespace WebApmDemo2.Models
{
    /// <summary>
    /// 记录SkyWalking的请求参数
    /// </summary>
    public class SkyWalkingFilterAttribute : TypeFilterAttribute
    {
        public SkyWalkingFilterAttribute() : base(typeof(Interna1Samp1eActionFi1ter))
        {
        }

        public class Interna1Samp1eActionFi1ter : IActionFilter
        {
            private readonly IEntrySegmentContextAccessor segContext;

            public Interna1Samp1eActionFi1ter(IEntrySegmentContextAccessor segContext)
            {
                this.segContext = segContext;
            }

            public void OnActionExecuting(ActionExecutingContext context)
            {
                var actionArgs = context.ActionArguments;
                foreach (var arg in actionArgs)
                {
                    var msgStr = JsonConvert.SerializeObject(arg.Value);
                    segContext.Context.Span.AddLog(LogEvent.Message($"请求参数{arg.Key} : { msgStr}"));
                }
            }

            public void OnActionExecuted(ActionExecutedContext context)
            {

            }
        }
    }

}
